<?


mysql_connect("localhost", "root", "root");
$result = mysql_list_tables("MyData");
$num_rows = mysql_num_rows($result);
for ($i = 0; $i < $num_rows; $i++) {

    echo "<?";
    //show query
    $queryInser = "INSERT INTO ";
    $queryInser = $queryInser . (mysql_tablename($result, $i));
    $queryInser = $queryInser . " (";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    //column  name
    if (mysql_num_rows($result2) > 0) {
        $index = 1;
        while ($row = mysql_fetch_row($result2)) {
            $index = $index + 1;
            if ($row[5] != "auto_increment") {
                if ($index > mysql_num_rows($result2)) {
                    $queryInser = $queryInser . ($row[0]);
                } else {
                    $queryInser = $queryInser . ($row[0]) . ",";
                }
            }
        }
    }
    $queryInser = $queryInser . " )";
    $queryInser = $queryInser . "VALUES (";
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }

    if (mysql_num_rows($result2) > 0) {
        $index = 1;
        while ($row = mysql_fetch_row($result2)) {
            $index = $index + 1;
            if ($row[5] != "auto_increment") {
                if ($index > mysql_num_rows($result2)) {
                    $queryInser = $queryInser . ($row[0]);
                } else {
                    $queryInser = $queryInser . ($row[0]) . ",";
                }
            }
        }
    }
    $queryInser = $queryInser . ")";

    //echo $queryInser;
    //echo "Table: ", mysql_tablename($result, $i), "\n";
    //entity class
    echo "\nclass " . strtolower(mysql_tablename($result, $i)) . "\n{";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    //column  name
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            print_r("\n    public $" . strtolower($row[0]) . ";");
        }
    }


    echo "\n}";


    /*
     * ----------------------------------------------------
     * query Insert
     * ----------------------------------------------------
     */
    //dao   class
    $queryInser = "INSERT INTO ";
    $queryInser = $queryInser . (mysql_tablename($result, $i));
    $queryInser = $queryInser . " ( ";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    //column  name
    if (mysql_num_rows($result2) > 0) {
        $index = 1;
        while ($row = mysql_fetch_row($result2)) {
            $index = $index + 1;
            if ($row[5] != "auto_increment") {
                if ($index > mysql_num_rows($result2)) {
                    $queryInser = $queryInser . " " . ($row[0]);
                } else {
                    $queryInser = " " . $queryInser . ($row[0]) . " , ";
                }
            }
        }
    }
    $queryInser = $queryInser . ")";
    $queryInser = $queryInser . " VALUES ( ";
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }

    if (mysql_num_rows($result2) > 0) {
        $index = 1;
        while ($row = mysql_fetch_row($result2)) {
            $index = $index + 1;
            if ($row[5] != "auto_increment") {
                if ($index > mysql_num_rows($result2)) {
                    $queryInser = $queryInser . "#" . ($row[0]) . "#";
                } else {
                    $queryInser = $queryInser . "#" . ($row[0]) . "#, ";
                }
            }
        }
    }
    $queryInser = $queryInser . ")";

    //echo $queryInser;
    /*
     * ----------------------------------------------------
     * generator entity class DAO
     * ----------------------------------------------------
     */

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }

    echo "\nclass " . strtolower(mysql_tablename($result, $i)) . "db\n{\n";
    //FUNCTION INSERT
    echo 'public static function insert(&$entity) {' . "\n";
    echo '$db = new dbhelper();' . "\n";
    echo '$query = "' . $queryInser . '"' . ";\n";
    echo '$db->setQuery($query);' . "\n";

    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));
            if ($row[5] != "auto_increment") {
                echo '$db->set_string_params("' . $row[0] . '", $entity->' . strtolower($row[0]) . ');';
                echo "\n";
            }
        }
    }
    echo 'return $db->excute();' . "\n";



    echo "\n}\n";
    /*
     * ----------------------------------------------------
     * query update
     * ----------------------------------------------------
     */
    $queryInser = "UPDATE ";
    $queryInser = $queryInser . (mysql_tablename($result, $i));
    $queryInser = $queryInser . " SET ";
    $queryWhere = " WHERE ";
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }

    if (mysql_num_rows($result2) > 0) {
        $index = 1;
        $index2nd = 0;
        while ($row = mysql_fetch_row($result2)) {
//            print_r($row);
            $index = $index + 1;
            if ($row[3] != "PRI" || $row[5] != "auto_increment") {
                if ($index > mysql_num_rows($result2)) {
                    $queryInser = $queryInser . ($row[0]) . " = " . "#" . ($row[0]) . "#";
                } else {
                    $queryInser = $queryInser . ($row[0]) . " = " . "#" . ($row[0]) . "#, ";
                }
            } else {

                if ($index2nd <= 1) {
                    $queryWhere = $queryWhere . ($row[0]) . " = " . "#" . ($row[0]) . "#";
                } else {
                    $queryWhere = $queryWhere . " AND " . ($row[0]) . " = " . "# " . ($row[0]) . "#";
                }
                $index2nd = $index2nd + 1;
            }
        }
    }
    $queryInser = $queryInser . $queryWhere;
    //echo $queryInser;
    //FUNCTION UPDATE----------------------------------

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    //echo "\nclass " . strtolower(mysql_tablename($result, $i)) . "db\n{\n";
    echo 'public static function update(&$entity) {' . "\n";
    echo '$db = new dbhelper();' . "\n";
    echo '$query = "' . $queryInser . '"' . ";\n";
    echo '$db->setQuery($query);' . "\n";

    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));

            echo '$db->set_string_params("' . $row[0] . '", $entity->' . strtolower($row[0]) . ');';
            echo "\n";
        }
    }
    echo 'return $db->excute();' . "\n";

    echo "    \n}\n";


    /*
     * ----------------------------------------------------
     * query get all
     * ----------------------------------------------------
     */
    $queryInser = "SELECT * FROM ";
    $queryInser = $queryInser . (mysql_tablename($result, $i));

    //echo "\nclass " . strtolower(mysql_tablename($result, $i)) . "db\n{\n";
    echo 'public static function get_all() {' . "\n";
    echo '$db = new dbhelper();' . "\n";
    echo '$query = "' . $queryInser . '"' . ";\n";
    echo '$db->setQuery($query);' . "\n";
    echo '$result = $db->excute();' . "\n";
    echo '$entity_arr = array();' . "\n";
    echo '$index = 0;' . "\n";
    echo ' while ($row = mysql_fetch_array($result)) {' . "\n";
    echo '$entity = new ' . strtolower(mysql_tablename($result, $i)) . '();' . "\n";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    if (mysql_num_rows($result2) > 0) {
        $index = 0;
        while ($row = mysql_fetch_row($result2)) {


            echo '$entity->' . strtolower($row[0]) . ' = $row[' . "'" . $row[0] . "'" . '];' . "\n";
        }
    }
    echo '$entity_arr[$index] = $entity;' . "\n";
    echo '$index = $index + 1;' . "\n";

    echo "\n}\n";
    echo 'return $entity_arr;' . "\n";


    echo "    \n}\n";


    $queryInser = "SELECT * FROM " . mysql_tablename($result, $i) . " WHERE ";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    if (mysql_num_rows($result2) > 0) {
        $index = 0;
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));
            if ($row[3] == "PRI") {
                if ($index >= 0) {
                    $queryInser = $queryInser . $row[0] . " = #" . $row[0] . "#";
                } else {
                    $queryInser = $queryInser . $row[0] . " = #" . $row[0] . "# AND";
                }
                //echo '$entity->' . strtolower($row[0]). ' = $row['."'".$row[0]."'".'];'."\n";
            }

            $index++;
        }
    }
    //echo "\nclass " . strtolower(mysql_tablename($result, $i)) . "db\n{\n";
    echo 'public static function get(&$entity) {' . "\n";
    echo '$db = new dbhelper();' . "\n";
    echo '$query = "' . $queryInser . '"' . ";\n";
    echo '$db->setQuery($query);' . "\n";


    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }

    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));
            if ($row[3] == "PRI") {
                echo '$db->set_string_params("' . $row[0] . '", $entity->' . strtolower($row[0]) . ');' . "\n";
            }
        }
    }
    echo '$result = $db->excute();' . "\n";

    echo 'while ($row = mysql_fetch_array($result)) {' . "\n";
    echo '$entity = new ' . strtolower(mysql_tablename($result, $i)) . '();' . "\n";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (!$result2) {
        echo 'Could not run query: ' . mysql_error();
        exit;
    }
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));

            echo '$entity->' . strtolower($row[0]) . ' = $row[' . "'" . $row[0] . "'" . '];' . "\n";
        }
    }

    echo 'return $entity;' . "\n";

    echo "    \n}\n";
    echo "    \n}\n";



    //SELECT * FROM CongTy WHERE Id = 1
    //-----------------------------------
    //generator  getby method
    //-----------------------------------

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            echo 'public static function get_by_' . strtolower($row[0]) . ' ($value) {' . "\n";
            $query = 'SELECT * FROM ' . mysql_tablename($result, $i) . ' WHERE ' . strtolower($row[0]) . ' = #' . strtolower($row[0]) . '#';
            echo '$db = new dbhelper();' . "\n";
            echo '$query = "' . $query . '"' . ";\n";
            echo '$db->setQuery($query);' . "\n";
            echo '$db->set_string_params($value);' . "\n";
            echo '$result = $db->excute();' . "\n";
            echo 'while ($row = mysql_fetch_array($result)) {' . "\n";
            echo '$entity = new ' . strtolower(mysql_tablename($result, $i)) . '();' . "\n";

            $result3 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
            if (mysql_num_rows($result3) > 0) {
                while ($row2 = mysql_fetch_row($result3)) {
                    //print_r("\n    $" . strtolower($row[0]));

                    echo '$entity->' . strtolower($row2[0]) . ' = $row[' . "'" . $row2[0] . "'" . '];' . "\n";
                }
            }


            echo 'return $entity;' . "\n";
            echo "    \n}\n";
            echo ' }' . "\n";
        }
    }

    //-----------------------------------
    //get data form post
    //-----------------------------------
    echo 'public static function get_post_data () {' . "\n";
    echo '$entity  = new ' . strtolower(mysql_tablename($result, $i)) . '();' . "\n";
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            //print_r("\n    $" . strtolower($row[0]));

            echo '$entity->' . strtolower($row[0]) . ' = $_POST["' . strtolower($row[0]) . '"];' . "\n";
        }
        echo 'return $entity;';
    }


    echo "\n}\n";


    $queryInser = "SELECT * FROM ";
    $queryInser = $queryInser . (mysql_tablename($result, $i));




    //-----------------------------------
    //make fulltext search  function
    //-----------------------------------
    echo 'public static function search($search_key) {' . "\n";
    echo '$arr = explode(" ",  trim($search_key));' . "\n";
    echo '$search = "";' . "\n";
    echo 'for ($i = 0; $i < count($arr); $i++) {' . "\n";
    echo ' if ($search == "") {
                $search = $search . " WHERE ";
                $search = $search . "(";
          ';
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (mysql_num_rows($result2) > 0) {
        $index = 0;
        while ($row = mysql_fetch_row($result2)) {
            if ($index <= 0 ) {
                echo '$search = $search ."' . strtolower($row[0]) . " LIKE '%" . '".$arr[$i].' . '"' . "%'" . '";' . "\n";
            } else  {
                echo '$search = $search ." || ' . strtolower($row[0]) . " LIKE '%" . '".$arr[$i].' . '"' . "%'" . '";' . "\n";
            }
            $index++;

        }
    }
    echo '$search = $search . ")";';
    echo '} else {
          $search = $search . " AND ";
          $search = $search . "(";
    ';
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (mysql_num_rows($result2) > 0) {
       $index = 0;
        while ($row = mysql_fetch_row($result2)) {
            if ($index <= 0 ) {
                echo '$search = $search ."' . strtolower($row[0]) . " LIKE '%" . '".$arr[$i].' . '"' . "%'" . '";' . "\n";
            } else  {
                echo '$search = $search ." || ' . strtolower($row[0]) . " LIKE '%" . '".$arr[$i].' . '"' . "%'" . '";' . "\n";
            }
            $index++;

        }
    }
    echo '$search = $search . ")";';
    echo "\n } \n";

    echo '$db = new dbhelper();' . "\n";
    echo '$query = "' . $queryInser . '". $search' . ";\n";
    echo '$db->setQuery($query);' . "\n";
    echo '$result = $db->excute();' . "\n";
    echo '$entity_arr = array();' . "\n";
    echo '$index = 0;' . "\n";
    echo ' while ($row = mysql_fetch_array($result)) {' . "\n";
    echo '$entity = new ' . strtolower(mysql_tablename($result, $i)) . '();' . "\n";

    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    if (mysql_num_rows($result2) > 0) {
        $index = 0;
        while ($row = mysql_fetch_row($result2)) {


            echo '$entity->' . strtolower($row[0]) . ' = $row[' . "'" . $row[0] . "'" . '];' . "\n";
        }
    }
    echo '$entity_arr[$index] = $entity;' . "\n";
    echo '$index = $index + 1;' . "\n";

    echo "\n}\n";
    echo 'return $entity_arr;' . "\n";


    echo "    \n}\n";
    echo "    \n}\n";


    echo "\n}\n";
echo "?>";


    //-----------------------------------
    //make input and update form
    //-----------------------------------
/*
  echo "\n".'<br/>'."\n";
    echo "---------------------------------\n";
  echo mysql_tablename($result, $i)."\n";
    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    echo '<br/>';
    echo '<form name="form' . mysql_tablename($result, $i) . '" action="" method="POST">
            <input type="hidden" value="add"/>
              <table>';
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            echo '
            <tr>
                <td>' . $row[0] . '</td>
                <td><input name="' . strtolower($row[0]) . '" type="text" value=""/></td>
            </tr>';
        }
    }

    echo '</table>
        <input type="submit" value="Save"/>
        </form>'."\n";




    $result2 = mysql_query("SHOW COLUMNS FROM " . mysql_tablename($result, $i));
    echo '<br/>'."\n";
    echo '<form name="form' . mysql_tablename($result, $i) . '" action="" method="POST">
            <input type="hidden" value="update"/>
              <table>';
    if (mysql_num_rows($result2) > 0) {
        while ($row = mysql_fetch_row($result2)) {
            echo '
            <tr>
                <td>' . $row[0] . '</td>
                <td><input name="' . strtolower($row[0]) . '" type="text" value=""/></td>
            </tr>';
        }
    }

    echo '</table>
        <input type="submit" value="Save"/>
        </form>';
     */
    
}

mysql_free_result($result);
?>
